/**
 * 语音处理API
 */

import { http, voiceRequest } from '@/utils/request'
import type { 
  SpeechToTextRequest, 
  SpeechToTextResponse,
  TextToSpeechRequest,
  TextToSpeechResponse,
  ProcessMessageRequest,
  Message
} from '@/types/chat'

// 用户录音上传响应类型
export interface UploadUserVoiceResponse {
  voiceUrl: string
  fileSize: number
  duration: number
}

// 用户录音上传请求类型
export interface UploadUserVoiceRequest {
  audioData: string  // Base64编码的音频数据
  format: string     // 音频格式
  userId: number     // 用户ID
  conversationId?: number  // 会话ID（可选）
}

// 语音转文字
export const speechToText = (data: SpeechToTextRequest) => 
  http.post<SpeechToTextResponse>('/voice/speech-to-text', data)

// 文字转语音
export const textToSpeech = (data: TextToSpeechRequest) => 
  http.post<TextToSpeechResponse>('/voice/text-to-speech', data)

// 完整语音对话流程
export const processVoiceMessage = (data: ProcessMessageRequest) => 
  http.post<Message>('/voice/process-message', data)

// 用户录音上传
export const uploadUserVoice = (data: UploadUserVoiceRequest) => 
  http.post<UploadUserVoiceResponse>('/voice/upload-user-audio', data)

// 完整录音流程接口
export interface CompleteVoiceFlowRequest {
  characterId: number
  userId: number
  audioData: string  // Base64编码的音频数据
  format: string     // 音频格式
  language: string   // 语言代码
  enableEmotionDetection: boolean
  conversationId?: number  // 会话ID（可选）
}

export interface CompleteVoiceFlowResponse {
  userVoiceUrl: string
  userVoiceSize: number
  userVoiceDuration: number
  recognizedText: string
  confidence: number
  emotionType: string
  emotionIntensity: number
  aiReply: {
    id: number
    conversationId: number
    senderType: number
    content: string
    contentType: string
    voiceUrl: string
    aiResponseTime: number
    createdAt: string
  }
  isMock: boolean
}

// 完整录音流程 - 使用语音专用请求实例
export const completeVoiceFlow = (data: CompleteVoiceFlowRequest) => 
  voiceRequest.post<CompleteVoiceFlowResponse>('/voice/complete-voice-flow', data)

// ========== 流式语音处理API ==========

// 创建语音处理任务请求类型
export interface CreateVoiceTaskRequest {
  audioData: string  // Base64编码的音频数据
  format: string     // 音频格式
  language: string   // 语言代码
  characterId: number
  userId: number
  conversationId?: number
  enableEmotionDetection: boolean
}

// 创建语音处理任务响应类型
export interface CreateVoiceTaskResponse {
  code: number
  message: string
  data: {
    taskId: string
    status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED'
    progress: number
    currentStep: string | null
    recognizedText: string | null
    aiReply: string | null
    voiceUrl: string | null
    errorMessage: string | null
    createdAt: string
    updatedAt: string
    estimatedTime: number
    wsUrl: string
  }
  timestamp: number
}

// 任务状态查询响应类型
export interface TaskStatusResponse {
  code: number
  message: string
  data: {
    taskId: string
    status: 'PENDING' | 'PROCESSING' | 'COMPLETED' | 'FAILED'
    progress: number
    currentStep: string | null
    recognizedText: string | null
    aiReply: string | null
    voiceUrl: string | null
    errorMessage: string | null
    createdAt: string
    updatedAt: string
  }
  timestamp: number
}

// 创建语音处理任务
export const createVoiceTask = (data: CreateVoiceTaskRequest) => 
  voiceRequest.post<CreateVoiceTaskResponse>('/voice/stream-task', data)

// 查询任务状态
export const getTaskStatus = (taskId: string) => 
  voiceRequest.get<TaskStatusResponse>(`/voice/task-status/${taskId}`)

// 取消任务
export const cancelTask = (taskId: string) => 
  voiceRequest.delete(`/voice/task/${taskId}`)

